﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/SystemManagement/Views/Shared/LigerUI.Master"
    Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    RoleAuthEdit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div id="toptoolbar">
    </div>
    <div style="width: 626px; height: 400px; margin: 0px; padding: 0px;">
        <div style="width: 300px; height: 380px; margin: 10px; margin-right: 1px; float: left;
            border: 1px solid #ccc; overflow: auto;">
            <ul id="tree1">
            </ul>
        </div>
        <div style="width: 300px; height: 380px; margin: 10px; margin-left: 1px; padding: 0;
            float: left; border: 1px solid #ccc; overflow: hidden;">
            <div id="maingrid" style="margin: 0; padding: 0">
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">
        body
        {
            overflow: hidden;
        }
    </style>
    <script language="javascript" type="text/javascript">
    var grid =null;
    var tree=null;
    var menuId=null;
    var roleId=<%=ViewData["roleId"]%>;
    var lastMenuId;
    var curMenuId;
    var datas=new Array();
    var buttons= new Array();


    jQuery(function () { 
    
            $("#toptoolbar").ligerToolBar({ items: [
                { text: '保存', click: f_save, icon: 'save' },
                { line: true },
                { text: '刷新', click: f_refresh, icon: 'refresh' },
                { line: true },
                { text: '关闭', click: f_close, icon: 'close' }
            ]
            });

            $("#tree1").ligerTree({
                nodeDraggable: false,
                idFieldName :'menuId',
                parentIDFieldName :'parentId',
                data: <%=ViewData["systemMenu"] %>,
                onSelect: onSelect
            });

            tree=$("#tree1").ligerGetTreeManager();


           grid= $("#maingrid").ligerGrid({
            columns: [{ display: '主键', name: 'buttonId', isAllowHide: false, hide: 1, width: 40 },
                    { display: '按钮名', name: 'cnName' , width:150 }],
                url: encodeURI('<%=Url.Action("GetMenuButtons","Menu")%>'+ '/?roleId=-1&menuId=-1' ), 
                sortName: 'roleId',
                //data: { Rows: []},
                dataAction: 'server',
                usePager: false,
                checkbox: true,
                rownumbers: true,
                width: '100%',
                height: '100%',
                InWindow:false,
                isChecked: function(rowdata)
                {
                    if (rowdata.ischecked == 'Y') 
                        return true;
                    return false;
                },
                onCheckRow :function(checked, data)
                {
                    if(checked)
                    {
                        data.ischecked='Y';
                        var button={menuId:curMenuId,buttonId:data.buttonId,ischecked:data.ischecked};
                        addButtons(button);
          
                    }else
                    {
                        data.ischecked='N';
                        var button={menuId:curMenuId,buttonId:data.buttonId,ischecked:data.ischecked};
                        addButtons(button);
                    }
                },
                onCheckAllRow:function(checked)
                {
                    
                    //alert(JSON.stringify(this.records));
                    if(checked)
                    {
                        for (var rowid in this.records)
                        {
                            if(this.records[rowid]["ischecked"]!="Y")
                            {
                                this.records[rowid]["ischecked"]="Y";
                                var data=this.records[rowid];
                                var button={menuId:curMenuId,buttonId:data.buttonId,ischecked:data.ischecked};
                                addButtons(button);
                            }

                        }

                    }else
                    {
                    
                        for (var rowid in this.records)
                        {

                            if(this.records[rowid]["ischecked"]!="N")
                            {
                                this.records[rowid]["ischecked"]="N";
                                var data=this.records[rowid];
                                var button={menuId:curMenuId,buttonId:data.buttonId,ischecked:data.ischecked};
                                addButtons(button);
                            }
                        }
                    }
                }
    
                //alternatingRow:true
            });
    })

    function addButtons(button)
    {
        for(var i =0;i<buttons.length;i++)
        {
            if(buttons[i].menuId == button.menuId &&buttons[i].buttonId==button.buttonId) 
            {

                if(buttons[i].ischecked==button.ischecked)
                {
                    return;
                }else
                {
                    buttons.splice(i,1);
                    return;
                }
            }
        }
        buttons.push(button);
    }


    function f_save()
    {

        var menus=new Array();
        notes= tree.getChecked();
        for (var i = 0; i < notes.length; i++)
        {

             menus.push({menuId:notes[i].data.menuId});

        }

        //alert(JSON.stringify(menus));
//        alert(JSON.stringify(buttons));


        $.ajax({
            type: "POST",
            url: '<%=Url.Action("SaveRoleAuthEdit","Role") %>',
            data: "menus="+JSON.stringify(menus)+"&buttons="+JSON.stringify(buttons)+"&roleId="+roleId,
            success: function(msg){
                $.ligerDialog.alert("保存成功！");
            }
        });
    }

    function f_refresh()
    {
        window.location.reload();
    }

    function f_close()
    {
        parent.w.close();
    }

    function onSelect(note)
    {
    
        if(note.data==null)
        return;
        
        curMenuId= note.data.menuId;
        datas[lastMenuId]=grid.getData();

        if($.trim(note.data.url)!="")
        {
            if(datas[curMenuId])
            {
                var jsonObj = {};
                jsonObj.Rows =datas[curMenuId];
                grid.set({data:jsonObj});
            }else
            {
                grid.options.url=encodeURI('<%=Url.Action("GetMenuButtons","Menu")%>' + '/?roleId='+roleId+'&menuId=' +curMenuId);
                grid.loadServerData (); 
                //alert(encodeURI('<%=Url.Action("GetMenuButtons","Menu")%>' + '/?roleId='+roleId+'&menuId=' +curMenuId));
            }

        }else
        {
            grid.set({data:{Rows:[]}});
        }

        lastMenuId=note.data.menuId;

    }


    </script>
</asp:Content>
